package com.poker.wiki.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.poker.common.exception.BusinessException;
import com.poker.common.exception.BusinessExceptionCode;
import com.poker.common.utils.CopyUtil;
import com.poker.common.utils.SnowFlake;
import com.poker.wiki.Service.UserService;
import com.poker.wiki.entity.User;
import com.poker.wiki.entity.UserExample;
import com.poker.wiki.mapper.UserMapper;
import com.poker.wiki.req.UserLoginReq;
import com.poker.wiki.req.UserQueryReq;
import com.poker.wiki.req.UserSaveReq;
import com.poker.wiki.resp.UserLoginResp;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    SnowFlake snowFlake;

    @Override
    public PageInfo<User> getAllUser(UserQueryReq req) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (!ObjectUtils.isEmpty(req.getName())){
            criteria.andNameLike("%"+req.getName()+"%");
        }
        if (!ObjectUtils.isEmpty(req.getId())){
            criteria.andIdEqualTo(req.getId());
        }

        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<User> userList= userMapper.selectByExample(example);
        PageInfo<User> pageInfo = new PageInfo<>(userList);

        return pageInfo;
    }

    @Override
    public void save(UserSaveReq req) {

        User user = CopyUtil.copy(req, User.class);

        if (ObjectUtils.isEmpty(user.getId())){
            // 判断用户名是否存在
            User u = selectByLoginName(req.getLoginName());
            if (ObjectUtils.isEmpty(u)) {
                user.setId(snowFlake.nextId());
                userMapper.insert(user);
            } else {
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }

        } else {
            userMapper.updateByPrimaryKey(user);
        }
    }

    @Override
    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public User selectByLoginName(String loginName) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andLoginNameEqualTo(loginName);
        List<User> userList = userMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public UserLoginResp login(UserLoginReq req) {
        User userDB = selectByLoginName(req.getLoginName());
        if (!ObjectUtils.isEmpty(userDB)) {
            // 用户名存在
            if (req.getPassword().equals(userDB.getPassword())) {
                // 密码正确
                UserLoginResp userLoginResp = CopyUtil.copy(userDB, UserLoginResp.class);
                return userLoginResp;
            } else {
                // 密码错误
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_ERROR);
            }
        } else {
            // 用户名不存在
            throw new BusinessException(BusinessExceptionCode.USER_LOGIN_ERROR);
        }
    }

}
